// Header 头部导航栏组件
<template>
  <div class="header">
    <div class="header-container">
      <div class="container">
        <div class="logo">
          <!-- 由于a标签会触发页面刷新，所以路由我们必须使用自带的标签 router-link -->
          <router-link :to="{ name: 'Home' }">
            <img src="../assets/vue.png" alt="" />
          </router-link>
        </div>
        <ul class="nav">
          <li>
            <router-link :to="{ name: 'Home' }">首页</router-link>
          </li>
          <li v-for="item in data" :key="item.typeId">
            <router-link
              :to="{
                name: 'Types',
                params: {
                  id: item.typeId, // 动态路由匹配 config
                },
              }"
              >{{ item.typeName }}</router-link
            >
          </li>
          <!-- <li v-for="item in headerType" :key="item.typeId">
            <router-link
              :to="{
                name: 'Types',
                params: {
                  id: item.typeId,// 动态路由匹配 config
                },
              }"
              >{{ item.typeName }}</router-link
            >
          </li> -->
        </ul>
        <div class="user">
          <!-- 三种情况 -->
          <!-- 1、正在登录中状态 -->
          <span v-if="isLogin">正在登录....</span>
          <!-- 2、登录成功状态 template在界面不会出现标签-->
          <template v-else-if="user">
            <a href="">{{ user.userName }}</a>
            <a href="">退出登录</a>
          </template>
          <!-- 3、未登录状态 -->
          <template v-else>
            <router-link :to="{ name: 'Login' }">登录</router-link>
            <router-link :to="{ name: 'Register' }">注册</router-link>
          </template>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
// import * as booksService from "@/services/BooksService";
import { mapState } from "vuex";
// var result = mapState("types", ["data", "isLoading"]);
// console.log(result);
export default {
  // data() {
  //   return {
  //     books: [],
  //   };
  // },
  async created() {
    // 界面创建时获得图书类型
    // console.log("界面创建了");
    // let resp = await booksService.getBookTypes();
    // console.log(resp);
    // this.books = resp;
  },
  computed: {
    //导航显示类型数量
    // headerType() {
    //   if (this.books != null) {
    //     return this.books.slice(0, 6);
    //   }
    // },
    //使用解构运算符直接分解原来对象中的属性到新对象中--从仓库取值
    ...mapState("types", ["data", "isLoading"]),
    ...mapState("login", {
      user: "data",
      isLogin: "isLoading",
    }),
  },
};
</script>

<style scoped>
.header {
  height: 60px;
}
.header-container {
  height: 60px;
  background: #000;
  color: #fff;
  line-height: 60px;
  position: fixed;
  z-index: 100;
  left: 0;
  top: 0;
  width: 100%;
  display: flex;
}
.container {
  display: flex;
}
.logo a {
  display: flex;
  align-items: center;
  height: 100%;
}
.logo img {
  width: 55px;
  height: 55px;
}
.nav {
  margin: 0 60px;
  display: flex;
  flex-grow: 1;
}
.nav a {
  display: block;
  padding: 0 30px;
}
.nav .router-link-exact-active {
  color: #fcb85f;
}
.user {
  font-size: 14px;
}
.user * {
  margin-left: 10px;
}
.header a {
  color: #fff;
}
</style>
